package com.zjitc.utils;/**
 * Create by IntelliJ IDEA
 *
 * @author:
 * @date-Time: 2018/4/5 22:51
 * @description:
 */
import com.zjitc.common.Gsonal;
import com.zjitc.common.redis.RedisCache;
import com.zjitc.dao.CategoryDao;
import com.zjitc.dao.OrderDao;
import com.zjitc.dao.ProductDao;
import com.zjitc.dao.UserDao;
import com.zjitc.pojo.*;


import java.time.LocalDateTime;

import java.util.List;

/**
 * Create by IntelliJ IDEA
 *
 * @author:
 * @date-Time: 2018/4/5 22:51
 * @description:
 */
@SuppressWarnings("ALL")
public class RedisAuto {

  private static final Integer DAY = 24 * 60 * 60;
  private static final Integer HOUR = 60 * 60;
  private static LocalDateTime localDateTime;

  public static void usersredis(UserDao dao, RedisCache redis, String name) {
    if (redis.exists(name)) {
      redis.del(name);
    }
    List<User> users = dao.findAll();
    users.forEach(item -> {
      String tojson = Gsonal.tojson(item);
      redis.hashset(name, item.getUsername(), tojson);
    });
    redis.expire(name, HOUR);
  }

  public static void categoryredis(CategoryDao dao, RedisCache redis, String name) {
    if (redis.exists(name)) {
      redis.del(name);
    }
    List<Category> categories = dao.findAll();
    categories.forEach(item -> {
      String tojson = Gsonal.tojson(item);
      redis.hashset(name, item.getCid(), tojson);
    });
    redis.expire(name, DAY);
  }

  public static void productredis(ProductDao dao, CategoryDao cdao, RedisCache redis, String name) {
    if (redis.exists(name)) {
      redis.del(name);
    }
    List<Product> findall = dao.findall();
    findall.forEach(product -> {
      Category byCid = cdao.findbycid(product.getCid());
      product.setCategory(byCid);
      String tojson = Gsonal.tojson(product);
      redis.hashset(name, product.getPid(), tojson);
    });
    redis.expire(name, DAY);
  }

  public static void hotsredis(ProductDao dao, RedisCache redis, String name) {
    if (redis.exists(name)) {
      redis.del(name);
    }
    List<Product> hots = dao.findHots();
    hots.forEach(product -> {
      String tojson = Gsonal.tojson(product);
      redis.hashset(name, product.getPid(), tojson);
    });
    redis.expire(name, DAY);
  }

  public static void newsredis(ProductDao dao, RedisCache redis, String name) {
    if (redis.exists(name)) {
      redis.del(name);
    }
    List<Product> news = dao.findNews();
    news.forEach(product -> {
      String tojson = Gsonal.tojson(product);
      redis.hashset(name, product.getPid(), tojson);
    });
    redis.expire(name, DAY);
  }

  public static void orderRedis(OrderDao dao, ProductDao pdao, RedisCache redis, String name) {
    List<Orders> orders = dao.findOrderbyuid(name);
    orders.forEach(item -> {
      List<Orderitems> listitem = dao.findOrderitem(item.getOid());
      listitem.forEach(orderitems -> {
        Product p = pdao.findbypid(orderitems.getPid());
        orderitems.setSubtotal(p.getShopPrice() * orderitems.getCount());
        orderitems.setProduct(p);
      });

      item.setOrderitems(listitem);

      String tojson = Gsonal.tojson(item);
      redis.hashset(name, item.getOid(), tojson);
    });
    redis.expire(name, DAY);

  }

  public static void orderRedisAll(OrderDao dao, ProductDao pdao, RedisCache redis, String name) {
    if(redis.exists(name)){
      redis.del(name);
    }
    List<Orders> ordersList = dao.findAll();
    ordersList.forEach(orders -> {
      List<Orderitems> listitem = dao.findOrderitem(orders.getOid());
      listitem.forEach(orderitems -> {
        Product p = pdao.findbypid(orderitems.getPid());
        orderitems.setSubtotal(p.getShopPrice() * orderitems.getCount());
        orderitems.setProduct(p);
      });
      orders.setOrderitems(listitem);
      String tojson = Gsonal.tojson(orders);
      redis.hashset(name, orders.getOid(), tojson);
    });
    redis.expire(name, DAY);
  }

  public static void inventoryredis(ProductDao productDao, RedisCache redis, String name) {
    if (redis.exists(name)) {
      redis.del(name);
    }
    List<Product> products = productDao.findall();
    products.forEach(product -> {
      Inventory inventory = new Inventory(product);
      redis.hashset(name,inventory.getPid(),Gsonal.tojson(inventory));
    });
    redis.expire(name,DAY);

  }


}
